$(function () {
    var signFun = function () {
        // var dateArray = [0, 2, 4, 6, 5]
        var dateArray = qdarr.split(",");
        var $dateBox = $("#js-qiandao-list"),
            $currentDate = $(".current-date"),
            $qiandaoBnt = $("#js-just-qiandao"),
            _html = '',
            _handle = true,
            myDate = new Date();
        $currentDate.text(myDate.getFullYear() + '年' + parseInt(myDate.getMonth() + 1) + '月' + myDate.getDate() + '日');
        var monthFirst = new Date(myDate.getFullYear(), parseInt(myDate.getMonth()), 1).getDay();
        var d = new Date(myDate.getFullYear(), parseInt(myDate.getMonth() + 1), 0);
        var totalDay = d.getDate();
        for (var i = 0; i < 42; i++) {
            _html += ' <li><div class="qiandao-icon"></div></li>'
        }
        $dateBox.html(_html)
        var $dateLi = $dateBox.find("li");
        for (var i = 0; i < totalDay; i++) {
            $dateLi.eq(i + monthFirst).addClass("date" + parseInt(i + 1));
            for (var j = 0; j < dateArray.length; j++) {
                if (i == dateArray[j]) {
                    $dateLi.eq(i + monthFirst).addClass("qiandao");
                }
            }
        }
        $(".date" + myDate.getDate()).addClass('able-qiandao');
        $dateBox.on("click", "li", function () {
            if ($(this).hasClass('able-qiandao') && _handle) {
                $(this).addClass('qiandao');
                qiandaoFun();
            }
        })
        $qiandaoBnt.on("click", function () {
            if (_handle) {
                qd_ajax(qduser);
                qiandaoFun();
            }
        });


        function qiandaoFun() {
            $qiandaoBnt.addClass('actived');
            openLayer("qiandao-active", qianDao);
            // _handle = false;
            // localStorage.setItem("chatQiandao", 'ok');
            var qddateTime = new Date();
            localStorage.setItem("chatQiandaoTime", qddateTime.getDate());
            _handle = true;
        }

        function qianDao() {
            $(".date" + myDate.getDate()).addClass('qiandao');
        }
    }();

    function openLayer(a, Fun) {
        $('.' + a).fadeIn(Fun)
    }
    var closeLayer = function () {
        $("body").on("click", ".close-qiandao-layer", function () {
            $(this).parents(".qiandao-layer").fadeOut()
        })
    }()
    $("#js-qiandao-history").on("click", function () {
        openLayer("qiandao-history-layer", myFun);
        history_ajax(qduser);
        // qiandaoFun()
        var qdoff = document.getElementsByClassName('qiandao-active')[0];
        console.log(qdoff);
        qdoff.style.display = 'none';

        function myFun() {
            console.log(1)
        }
    })

    //签到请求
    function qd_ajax(obj) {
        var fd = new FormData();
        fd.append('name', obj);
        $.ajax({
            url: '/user/qd',
            type: 'POST',
            timeout: 3000,
            dataType: 'JSON',
            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
            data: fd,
            contentType: false, //不设置内容类型
            processData: false, //不处理数据
            success: function (data) {
                if (data.code == 200) {
                    document.getElementById('qd4').innerHTML = data.msg;
                    document.getElementById('qd5').innerHTML = data.data;
                } else {
                    document.getElementById('qd4').innerHTML = data.msg;
                    document.getElementById('qd5').innerHTML = data.data;
                }
            },
            error: function (XMLResponse) {
                console.log(XMLResponse.responseText);
            }
        });
    }

    //签到历史请求
    function history_ajax(obj) {
        var fd = new FormData();
        fd.append('name', obj);
        $.ajax({
            url: '/history/signin',
            type: 'POST',
            timeout: 3000,
            dataType: 'JSON',
            contentType: 'application/x-www-form-urlencoded;charset=utf-8',
            data: fd,
            contentType: false, //不设置内容类型
            processData: false, //不处理数据
            success: function (data) {

                if (data.code == 200) {
                    var qd_code = '';
                    data.data.jl.forEach(element => {
                        qd_code = qd_code + "<tr> <td>" + element.time + "</td> <td>" + element.jl + "</td> <td>签到奖励</td> </tr>";
                    });
                    document.getElementById('qd1').innerHTML = data.data.by;
                    document.getElementById('qd2').innerHTML = data.data.zj;
                    document.getElementById('qd3').innerHTML = data.data.lj;
                } else {
                    var qd_code = '';
                }
                document.getElementById('qdjl').innerHTML = qd_code;
            },
            error: function (XMLResponse) {
                console.log(XMLResponse.responseText);
            }
        });
    }

})
